﻿#region 命名空间

using System;
using System.Text;
using System.Web;
using System.Collections.Generic;
using System.Web.UI;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using WinnerCMS.Data;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.Model.Enum;
using TreeNode = WinnerCMS.Model.TreeNode;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// SpecialManage 的摘要说明。
    /// </summary>
    public class SpecialManage
    {
        private static readonly SpecialDal MySpecialDAL;
        private static readonly Dictionary<int, Special> Alllist;

        #region 构造函数

        static SpecialManage()
        {
            MySpecialDAL = new SpecialDal();
            Alllist = SpecialDal.Alllist;
            foreach (var kp in Alllist)
            {
                kp.Value.Link = StaticFilePath.SpecialUrl(kp.Value);
            }
        }

        #endregion

        #region 添加专题

        /// <summary>
        /// 添加专题
        /// </summary>
        /// <param name="model"></param>
        public Result Save(Special model)
        {
            Result resu = new Result();

            #region   验证

            if (model.SpecialName.Length == 0)
            {
                resu.Errors = "必须填写专题名称";
                return resu;
            }

            if (model.SpecialFilePostfix.Length == 0 && model.SpecialURLStyle == 0)
            {
                resu.Errors = "“专题页扩展名”不能为空！";
                return resu;
            }

            #endregion

            Log LogSpecial = new Log();
            LogSpecial.Type = "专题管理";
            if (model.Id == 0)
            {
                MySpecialDAL.Add(model);
                LogSpecial.Event = "添加专题" + model.SpecialName;
            }
            else
            {
                MySpecialDAL.Edit(model);
                LogSpecial.Event = "修改专题，ID：" + model.Id + " 为 " + model.SpecialName;
            }


            LogManage.Add(LogSpecial);

            return resu;
        }

        #endregion

        #region 加载

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="SpecialID"></param>
        /// <returns></returns>
        public Special GetInfo(int SpecialID)
        {
            return MySpecialDAL.GetInfo(SpecialID);
        }

        #endregion

        /// <summary>
        /// 专题排序
        /// </summary>
        /// <param name="specialId"></param>
        /// <param name="type"></param>
        public void OrderByMove(int specialId, string type)
        {
            MySpecialDAL.OrderByMove(specialId, type);

            Log LogSpecial = new Log();
            LogSpecial.Type = "专题管理";
            LogSpecial.Event = "专题排序ID" + specialId + ",排序动作" + type;
            LogManage.Add(LogSpecial);
        }

        /// <summary>
        /// 清空该专题的文章
        /// </summary>
        /// <param name="specialId"></param>
        /// <param name="modelId"></param>
        public void Clear(string specialId, byte modelId, UpdatePanel UpdatePanel1)
        {
            MySpecialDAL.Clear(specialId, modelId.ToString(), ModelManage.GetTableName(modelId));

            Log LogSpecial = new Log();
            LogSpecial.Type = "专题管理";
            LogSpecial.Event = "清空专题ID" + specialId + "下的数据";
            LogManage.Add(LogSpecial);

            MsgBox.Message("已经该专题彻底清空！", UpdatePanel1);
        }

        /// <summary>
        /// 删除专题
        /// </summary>
        /// <param name="specialId"></param>
        /// <param name="modelId"></param>
        public void Del(int specialId, byte modelId, UpdatePanel UpdatePanel1)
        {
            if (MySpecialDAL.HaveData(specialId) >= 1)
            {
                MsgBox.Message("该专题下还有数据存在，请先清空专题！", UpdatePanel1);
            }
            else
            {
                MySpecialDAL.Delete(specialId);

                Log LogSpecial = new Log();
                LogSpecial.Type = "专题管理";
                LogSpecial.Event = "删除专题" + specialId;
                LogManage.Add(LogSpecial);

                MsgBox.Message("删除成功！", UpdatePanel1);
            }
        }

        #region 读取数据库里所有专题

        /// <summary>
        /// 读取数据库里所有专题
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, Special> GetInfos()
        {
            return Alllist;
        }

        #endregion

        #region 专题js数据源

        /// <summary>
        /// 专题js数据源
        /// </summary>
        /// <param name="id">专题ID</param>
        /// <returns></returns>
        public IList<TreeNode> SpecialTreeList(int id)
        {
            IEnumerable<Special> all = GetInfos().Values;
            IList<TreeNode> tns = new List<TreeNode>();
            foreach (var v in all.Where(x => x.ParentId == id).OrderBy(x => x.OrderId))
            {
                tns.Add(new TreeNode(v.Id.ToString(), v.SpecialName)
                {
                    State = all.Any(x => x.ParentId == v.Id) ? "closed" : "open",
                    CustomField1 = v.Id.ToString(),
                    CustomField2 = v.Id.ToString(),
                    //CustomField3 = v.URLStyle.ToString(),
                    CustomField4 = v.SpecialURLStyle.ToString(),
                    CustomField5 = v.Id.ToString()
                });
            }

            return tns;
        }

        #endregion

        #region 绑定专题控件

        /// <summary>
        /// 绑定专题控件
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Type"></param>
        /// <param name="strNewListItem"></param>
        public void BindDDL(ListControl Name, BindOption Type, string strNewListItem, byte? ModelId)
        {
            Name.DataSource = MySpecialDAL.Bind_DDL(Type, ModelId);
            Name.DataTextField = "SpecialName";
            Name.DataValueField = "ID";
            Name.DataBind();

            Name.Items.Insert(0, new ListItem(strNewListItem, "0"));
        }

        #endregion
    }
}